package top.laoshuzi.server.common.bean

import com.fasterxml.jackson.annotation.JsonInclude
import top.laoshuzi.server.common.exception.BizException

@JsonInclude(value = JsonInclude.Include.NON_NULL)
class ResponseResult<T> {
    var isSuccess: Boolean = false
    var error: ResponseError? = null
    var result: T? = null
}

object ResponseResultUtils {

    fun successed(): ResponseResult<Any> {
        return successed(null)
    }

    fun <T> successed(t: T?): ResponseResult<T> {
        val result = ResponseResult<T>()
        result.isSuccess = true
        result.result = t
        return result
    }

    fun failed(error: ResponseError): ResponseResult<*> {
        val result = ResponseResult<Any>()
        result.isSuccess = false
        result.error = error
        return result
    }

    fun failed(exception: BizException): ResponseResult<*> {
        return failed(ResponseError(exception.errorCode, exception.errorMessage))
    }

}


